<template>
  <div>
    <h1>采购标书</h1>
    <br>
    <!--下拉列表-->
    <!--placeholder 提示信息-->


    <el-select v-model="supplier" placeholder="请选择是否中标">
      <el-option label="全部采购" value="-1">
      </el-option>
      <el-option label="未中标" value="0">
      </el-option>
      <el-option label="已中标" value="1">
      </el-option>
    </el-select>
    <el-button type="primary" @click="querySale()">查询</el-button>


    <br><br>
    <span>采购标书</span>
    <el-row>
      <el-table :data="goodsList" stripe>
        <el-table-column prop="poNumber" label="采购标书编号"></el-table-column>
        <el-table-column prop="goodsName" label="采购商品名字"></el-table-column>
        <el-table-column prop="goodsType" label="采购商品类型"></el-table-column>
        <el-table-column prop="prGoodsDetail" label="采购商品规格"></el-table-column>
        <!--        <el-table-column prop="goodsNum" label="采购商品数量"></el-table-column>-->
        <el-table-column prop="poState" label="是否中标">
          <template #default="scope">
            {{ this.covertStatus(scope.row.poState) }}
          </template>
        </el-table-column>
        <el-table-column label="">
          <template #default="scope">
            <el-button v-if="scope.row.poState===0" type="primary" @click="priceBook(scope.row)">报价</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination v-model:current-page="curPage" v-model:page-size="pageSize"
                     background :total="total" @current-change="changeCurPage()"></el-pagination>
    </el-row>
    <br>

    <br><br>


    <el-dialog v-model="pick" title="报价商品" style="width: 500px">

      <el-form :model="form" ref="form">
        <el-form-item label="采购书编号">
          <el-input v-model="form.poNumber" disabled></el-input>
        </el-form-item>
        <el-form-item label="商品编号">
          <el-input v-model="form.goodsNumber " @change="queryInventory()"></el-input>
          <el-select v-model="form.goodsNumber" @change="queryInventory()" placeholder="商品编号">
            <el-option
                v-for="item in lessonArray"
                :key="item.goodsDetailId"
                :label="item.goodsCode"
                :value="item.goodsCode">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品类型">
          <el-input v-model="form.goodsType " disabled></el-input>
        </el-form-item>

        <el-form-item label="商品名字">
          <el-input v-model="form.goodsName" :disabled="formlist"></el-input>
        </el-form-item>
        <el-form-item label="商品规格">
          <el-input v-model="form.goodsDetail" :disabled="formlist"></el-input>
        </el-form-item>

        <el-form-item label="报价价格">
          <el-input v-model="form.sellPrice"></el-input>
        </el-form-item>


      </el-form>


      <el-button type="primary" @click="determine()">确定</el-button>

    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import {ElMessage} from "element-plus";

export default {
  name: "SalesIssue",
  created() {
    this.querySale()
    axios.post("/api/user/query", null).then(res => {
      this.userName = res.data

    })

    axios.post("/api/client/selectClientName", null).then(res => {
      this.lessonArray = res.data
    })

  },
  data() {
    return {
      formlist: false,
      goodsList: [],
      supplier: '-1',
      userName: '',
      price: 0,
      pick: false,
      purchaseInfoList: [],
      location: '',
      lessonArray: [],
      form: {
        poNumber: "",
        suppllierName: "",
        goodsNumber: "",
        goodsDetail: "",
        sellPrice: "",
        goodsName: "",
        goodsType: ""

      }

    }

  }, methods: {
    queryInventory() {
      let param = new URLSearchParams();
      param.append("goodsNumber", this.form.goodsNumber)
      this.form.goodsName = []
      this.form.goodsDetail = []
      axios.post("/apf/goodsDetail/selectByGoodsNumber", param).then(res => {
        if (res.data != null && "" != res.data) {

          this.form.goodsType = res.data.goodsType
          this.form.goodsName = res.data.goodsName
          this.form.goodsDetail = res.data.goodsSpecification

          /*  this.imageUrl=servicePath+res.data.img*/
          this.formlist = true

        } else {
          this.formlist = true
        }

      })


    },


    covertStatus(statusCode) {

      if (statusCode === 0) {
        return '待投标'
      }
      if (statusCode === 1) {
        return '已中标'
      }
      if (statusCode === 2) {
        return '未中标'
      }
    },
    addSQE() {
      this.pick = true
      axios.post("/api/CommodityRepertory/queryByAll", null).then(res => {
        this.purchaseInfoList = res.data

      })


    },
    put() {
      let param = {
        "clientName": this.supplier,
        "salesman": this.userName,
        "sellPrice": this.price,
        "location": this.location

      }
      if (this.supplier != null && this.supplier != '' && this.location != null && this.location != '') {
        axios.post("/api/sale/put", param).then(res => {
          if (res.data == "ok") {
            ElMessage({
              message: '出库成功',
              type: 'success'
            })
            this.querySale()
          } else if (res.data == "库存不足") {
            ElMessage({
              message: '库存不足',
              type: 'success'
            })
          } else {
            ElMessage({
              message: '出库数量错误',
              type: 'success'
            })
          }
        })
      } else {
        ElMessage({
          message: '请选择客户和填写地址',
          type: 'success'
        })
      }


    },
    priceBook(scope) {
      this.pick = true
      this.form.poNumber = scope.poNumber
     /* this.form.goodsType = scope.goodsType*/
      axios.post("/apf/goodsDetail/selectAllGoodsDetail", null, {
        'headers': {'token': localStorage.getItem("token")}
      }).then(res => {
        this.lessonArray = res.data
      })


    },
    addGoods(scope) {

      axios.post("/api/sale/addSale", scope).then(res => {
        this.querySale()
      })


    },
    determine() {
      this.pick = false
      axios.post("/apf/poBook/createPriceBook", this.form, {
        'headers': {'token': localStorage.getItem("token")}
      }).then(res => {

      })
    },
    querySale() {
      this.price = 0;
      let param = new URLSearchParams()
      param.append("state", this.supplier)


      axios.post("/apf/poBook/queryPoBook", param, {
        'headers': {'token': localStorage.getItem("token")}
      }).then(res => {
        this.goodsList = res.data

        this.goodsList.forEach(c => {
          //  this.price+=c.salePrice*c.saleAmount
          console.log(this.price)
        })


      })

    },
    change(scope) {

      if (scope.saleAmount > 0) {
        axios.post("/api/sale/update", scope).then(res => {
          if (scope.saleAmount > res.data) {
            ElMessage({
              message: '库存不足，库存数量为' + res.data,
              type: 'success'
            })


          }
          this.querySale()


        })
      } else {
        this.price = 0
        ElMessage({
          message: '数量错误',
          type: 'success'
        })
      }


    }

  }
}
</script>

<style scoped>

</style>